package org.zjframework.traceframe.common.dao.util;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.sql.Blob;
import java.util.List;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import javax.servlet.http.HttpServletResponse;


/*import com.hitek.modelue.users.model.Users;*/
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;


public class ExprotToolUtil {
	
	public static void createXML(HttpServletResponse response, List<?> list,
			OutputStream out, String fname,Class context) throws Exception{
		
		response.reset();
		// 生成csv文件
		response.setHeader("Content-disposition", "attachment;filename="
				+ fname + ".xml");
		response.setContentType("application/xml");
		response.setCharacterEncoding("UTF-8");
		try {
			out = response.getOutputStream();
			out.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
					.getBytes());
			out.write("\n<root>".getBytes());
			for (int i = 0; i < list.size(); i++) {
				/*Users p = (Users) list.get(i);*/
				/*out.write("\n\t<Users>".getBytes());
				out.write("\n\t\t<id>".getBytes());
				out.write(p.getId().getBytes());
				out.write("</id>".getBytes());
				out.write("\n\t\t<name>".getBytes());
				out.write(p.getUsername().getBytes());
				out.write("</name>".getBytes());
				out.write("\n\t</Users>".getBytes());*/
			}
			out.write("\n</root>".getBytes());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
		public static void createEXCEL(HttpServletResponse response, List<?> list,
				OutputStream out, String fname,Class context,String sheetname) throws Exception{
		
		response.reset();
		// 生成xls文件
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-disposition", "attachment;filename="
				+ fname + ".xls");
		try {
			Field[] field = context.getDeclaredFields();
			Method[] method = context.getDeclaredMethods();
			out = response.getOutputStream();
			Label l = null;
			WritableWorkbook wbook = Workbook.createWorkbook(out);
			// 写sheet名称
			WritableSheet sheet = wbook.createSheet(sheetname, 0);
			WritableFont wfc4 = new WritableFont(
					WritableFont.ARIAL, 9, WritableFont.NO_BOLD, false,
					jxl.format.UnderlineStyle.NO_UNDERLINE,
					jxl.format.Colour.BLACK);
			jxl.write.WritableCellFormat wcfFC4 = new jxl.write.WritableCellFormat(
					wfc4);
			wcfFC4.setBackground(jxl.format.Colour.LIGHT_GREEN);
			int col = 0;
			sheet.setColumnView(col, 12);
			/*for(Field fields : field){
				l = new Label(col, 0, fields.getName(), wcfFC4);
				sheet.addCell(l);
				col++;
			}*/
			
			if(sheetname.equals("客户信息")){
				l = new Label(0, 0, "ID", wcfFC4);
				sheet.addCell(l);
				Label l1 = new Label(1, 0, "客户助记码", wcfFC4);
				sheet.addCell(l1);
				Label l2 = new Label(2, 0, "客户名称", wcfFC4);
				sheet.addCell(l2);
				Label l3 = new Label(3, 0, "客户编号", wcfFC4);
				sheet.addCell(l3);
				Label l4 = new Label(4, 0, "客户地址", wcfFC4);
				sheet.addCell(l4);
				Label l5 = new Label(5, 0, "所购产品", wcfFC4);
				sheet.addCell(l5);
				Label l6 = new Label(6, 0, "服务开始时间", wcfFC4);
				sheet.addCell(l6);
				Label l7 = new Label(7, 0, "服务结束时间", wcfFC4);
				sheet.addCell(l7);
				Label l8 = new Label(8, 0, "服务编号", wcfFC4);
				sheet.addCell(l8);
				Label l9 = new Label(9, 0, "行业", wcfFC4);
				sheet.addCell(l9); 
				Label l10 = new Label(10, 0, "实施顾问", wcfFC4);
				sheet.addCell(l10);
				Label l11 = new Label(11, 0, "服务专员", wcfFC4);
				sheet.addCell(l11);
				Label l12 = new Label(12, 0, "第一联系人", wcfFC4);
				sheet.addCell(l12);
				Label l13 = new Label(13, 0, "第一联系人职务", wcfFC4);
				sheet.addCell(l13);
				Label l14 = new Label(14, 0, "第一联系人电话", wcfFC4);
				sheet.addCell(l14);
				Label l15 = new Label(15, 0, "第一联系人邮箱", wcfFC4);
				sheet.addCell(l15);
				Label l16 = new Label(16, 0, "第一联系人备注", wcfFC4);
				sheet.addCell(l16);
				Label l17 = new Label(17, 0, "第二联系人", wcfFC4);
				sheet.addCell(l17);
				Label l18 = new Label(18, 0, "第二联系人职务", wcfFC4);
				sheet.addCell(l18);
				Label l19 = new Label(19, 0, "第二联系人电话", wcfFC4);
				sheet.addCell(l19);
				Label l20 = new Label(20, 0, "第二联系人邮箱", wcfFC4);
				sheet.addCell(l20);
				Label l21 = new Label(21, 0, "第二联系人备注", wcfFC4);
				sheet.addCell(l21);
				Label l22 = new Label(22, 0, "第三联系人", wcfFC4);
				sheet.addCell(l22);
				Label l23 = new Label(23, 0, "第三联系人职务", wcfFC4);
				sheet.addCell(l23);
				Label l24 = new Label(24, 0, "第三联系人电话", wcfFC4);
				sheet.addCell(l24);
				Label l25 = new Label(25, 0, "第三联系人邮箱", wcfFC4);
				sheet.addCell(l25);
				Label l26 = new Label(26, 0, "第三联系人备注", wcfFC4);
				sheet.addCell(l26);
				Label l27 = new Label(27, 0, "客户类型", wcfFC4);
				sheet.addCell(l27);
				
			}
			if(sheetname.equals("需求跟踪")){
				l = new Label(0, 0, "ID", wcfFC4);
				sheet.addCell(l);
				Label l1 = new Label(1, 0, "客户名称", wcfFC4);
				sheet.addCell(l1);
				Label l2 = new Label(2, 0, "客户编号", wcfFC4);
				sheet.addCell(l2);
				Label l3 = new Label(3, 0, "所购模块", wcfFC4);
				sheet.addCell(l3);
				Label l4 = new Label(4, 0, "需求编号", wcfFC4);
				sheet.addCell(l4);
				Label l5 = new Label(5, 0, "添加记录的时间", wcfFC4);
				sheet.addCell(l5);
				Label l6 = new Label(6, 0, "需求模块", wcfFC4);
				sheet.addCell(l6);
				Label l7 = new Label(7, 0, "发起人", wcfFC4);
				sheet.addCell(l7);
				Label l8 = new Label(8, 0, "需求描述", wcfFC4);
				sheet.addCell(l8);
				Label l9 = new Label(9, 0, "解决问题", wcfFC4);
				sheet.addCell(l9); 
				Label l10 = new Label(10, 0, "跟踪人", wcfFC4);
				sheet.addCell(l10);
				Label l11 = new Label(11, 0, "预计解决时间", wcfFC4);
				sheet.addCell(l11);
				Label l12 = new Label(12, 0, "联系人", wcfFC4);
				sheet.addCell(l12);
				Label l13 = new Label(13, 0, "联系人电话", wcfFC4);
				sheet.addCell(l13);
				Label l14 = new Label(14, 0, "需求状态	", wcfFC4);
				sheet.addCell(l14);
				Label l15 = new Label(15, 0, "是否作废", wcfFC4);
				sheet.addCell(l15);
			}
			if(sheetname.equals("问题")){
				l = new Label(0, 0, "ID", wcfFC4);
				sheet.addCell(l);
				Label l1 = new Label(1, 0, "客户名称", wcfFC4);
				sheet.addCell(l1);
				Label l2 = new Label(2, 0, "服务开始时间", wcfFC4);
				sheet.addCell(l2);
				Label l3 = new Label(3, 0, "服务结束时间", wcfFC4);
				sheet.addCell(l3);
				Label l4 = new Label(4, 0, "问题编号", wcfFC4);
				sheet.addCell(l4);
				Label l5 = new Label(5, 0, "问题类型", wcfFC4);
				sheet.addCell(l5);
				Label l6 = new Label(6, 0, "问题描述", wcfFC4);
				sheet.addCell(l6);
				Label l7 = new Label(7, 0, "解决办法", wcfFC4);
				sheet.addCell(l7);
				Label l8 = new Label(8, 0, "期望解决日期", wcfFC4);
				sheet.addCell(l8);
				Label l9 = new Label(9, 0, "提出人", wcfFC4);
				sheet.addCell(l9); 
				Label l10 = new Label(10, 0, "联系人邮箱", wcfFC4);
				sheet.addCell(l10);
				Label l11 = new Label(11, 0, "联系电话", wcfFC4);
				sheet.addCell(l11);
				Label l12 = new Label(12, 0, "提出时间", wcfFC4);
				sheet.addCell(l12);
				Label l13 = new Label(13, 0, "当前处理人", wcfFC4);
				sheet.addCell(l13);
				Label l14 = new Label(14, 0, "关闭日期", wcfFC4);
				sheet.addCell(l14);
				Label l15 = new Label(15, 0, "服务专员", wcfFC4);
				sheet.addCell(l15);
				Label l16 = new Label(16, 0, "问题状态", wcfFC4);
				sheet.addCell(l16);
				Label l17 = new Label(17, 0, "时间戳", wcfFC4);
				sheet.addCell(l17);
				Label l18 = new Label(18, 0, "假删标记", wcfFC4);
				sheet.addCell(l18);
			}
			if(sheetname.equals("我的回访") || sheetname.equals("回访计划")){
				l = new Label(0, 0, "ID", wcfFC4);
				sheet.addCell(l);
				Label l1 = new Label(1, 0, "客户名称", wcfFC4);
				sheet.addCell(l1);
				Label l2 = new Label(2, 0, "服务开始时间", wcfFC4);
				sheet.addCell(l2);
				Label l3 = new Label(3, 0, "服务结束时间", wcfFC4);
				sheet.addCell(l3);
				Label l4 = new Label(4, 0, "回访时间", wcfFC4);
				sheet.addCell(l4);
				Label l5 = new Label(5, 0, "回访人", wcfFC4);
				sheet.addCell(l5);
				Label l6 = new Label(6, 0, "回访内容", wcfFC4);
				sheet.addCell(l6);
				Label l7 = new Label(7, 0, "客户反馈", wcfFC4);
				sheet.addCell(l7);
				Label l8 = new Label(8, 0, "客户联系人", wcfFC4);
				sheet.addCell(l8);
				Label l9 = new Label(9, 0, "联系人电话", wcfFC4);
				sheet.addCell(l9); 
				Label l10 = new Label(10, 0, "职务", wcfFC4);
				sheet.addCell(l10);
				Label l11 = new Label(11, 0, "联系人邮箱", wcfFC4);
				sheet.addCell(l11);
				Label l12 = new Label(12, 0, "回访次数", wcfFC4);
				sheet.addCell(l12);
				Label l13 = new Label(13, 0, "上次回访时间", wcfFC4);
				sheet.addCell(l13);
				Label l14 = new Label(14, 0, "计划回访时间", wcfFC4);
				sheet.addCell(l14);
				Label l15 = new Label(15, 0, "是否超期", wcfFC4);
				sheet.addCell(l15);
				Label l16 = new Label(16, 0, "实际回访时间", wcfFC4);
				sheet.addCell(l16);
				Label l17 = new Label(17, 0, "添加时间", wcfFC4);
				sheet.addCell(l17);
				Label l18 = new Label(18, 0, "状态", wcfFC4);
				sheet.addCell(l18);
			}
			// 设置字体样式
			WritableFont wfc5 = new WritableFont(
					WritableFont.ARIAL, 9, WritableFont.NO_BOLD, false,
					jxl.format.UnderlineStyle.NO_UNDERLINE,
					jxl.format.Colour.BLACK);
			jxl.write.WritableCellFormat wcfFC5 = new jxl.write.WritableCellFormat(
					wfc5);
			for (int i = 0; i < list.size(); i++) {
				Object obj = context.newInstance();
				obj = (Object) list.get(i);
				int j = 0;
				for (int k = 0; k < field.length; k++) {
					for(Method methods : method){
						if(methods.getName().equalsIgnoreCase("get" + field[k].getName())){
							if(methods.getName().equalsIgnoreCase("get" + "image1") 
								&& 
								methods.invoke(obj, null)!=null){
								InputStream is = ((Blob)methods.invoke(obj, null)).getBinaryStream();
								byte []b = new byte[20480];
								int len = 0;
								String a = "";
								while ((len = is.read(b)) != -1) {
										a = a + b.toString();
								}
								l = new Label(j, i + 1,  
										(methods.invoke(obj, null)!=null?
												a:"无"), wcfFC5);
								sheet.addCell(l);
								j++;
							}else{
								l = new Label(j, i + 1,  methods.invoke(obj, null)!=null?methods.invoke(obj, null).toString():"无", wcfFC5);
								sheet.addCell(l);
								j++;
							}
						}
					}
				}
			}
			// 写入流中
			wbook.write();
			wbook.close();

			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public static void createPDF(HttpServletResponse response, List<?> list,
			OutputStream out, String fname,Class context) throws Exception {
		try {
			Field[] fields = context.getDeclaredFields();
			Method[] methods = context.getDeclaredMethods();
			response.reset();
			out = response.getOutputStream();
			Document document = new Document(PageSize.A4, 50, 50, 50, 50);
			response.setContentType("application/pdf");
			response.setHeader("Content-disposition", "attachment;filename="
					+ fname + ".pdf");
			ByteArrayOutputStream baos = new ByteArrayOutputStream();

			PdfWriter.getInstance(document, out);
			document.open();
			int cols = list.size();
			// 创建PDF表格
			PdfPTable table = new PdfPTable(fields.length);
			// 设置pdf表格的宽度
			table.setTotalWidth(500);
			// 设置是否要固定其宽度
			table.setLockedWidth(true);
			// 表头字体
			Font thfont = new Font();
			// 设置表头字体的大小
			thfont.setSize(7);
			// 设置表头字体的样式
			thfont.setStyle(Font.BOLD);
			Font tdfont = new Font();
			tdfont.setSize(7);
			tdfont.setStyle(Font.NORMAL);
			// 设置水平对齐方式
			table.setHorizontalAlignment(Element.ALIGN_MIDDLE);
			// 设置table的header
			for(int i=0; i<fields.length; i++) {
				table.addCell(new Paragraph(fields[i].getName(), thfont));
			}
			// 循环设置table的每一行
			for (int listlen = 0; listlen < list.size(); listlen++) {
				Object obj = context.newInstance();
				obj = (Object) list.get(listlen);
				for (int i = 0; i < fields.length; i++) {
					for (int j = 0; j < methods.length; j++) {
						if (methods[j].getName().equalsIgnoreCase(
								"get" + fields[i].getName())) {
							String value =formatType(methods[j].invoke(obj, null), methods[j].getGenericReturnType());
							table.addCell(new Paragraph( value, tdfont));
						}
					}
				}
			}
			document.add(table);
			document.close();
			// baos.writeTo(out);
			baos.close();
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("");
		}
	}

	
	public static String formatType(Object obj, Type type) {
		String name = obj.getClass().getName( );
		if( "java.lang.String".equals(name)) {
		return obj.toString();
		} else if( "java.lang.Integer".equals(name) || (type == Integer.TYPE) ) {
			return String.valueOf(obj);
		}
		return obj.toString();
	}
}
